package com.example.controller;


import com.example.Utils.LoggerUtils;
import com.example.Utils.UserHolder;
import com.example.model.Result;
import com.example.model.User;
import com.example.model.UserOwn;
import com.example.modelDTO.UserOwnDTO;
import com.example.service.impl.ProductServiceImpl;
import com.example.service.impl.UserOwnServiceImpl;
import io.swagger.models.auth.In;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/userOwn")
public class UserOwnController {
    private static final Logger logger = LoggerUtils.getLogger(UserOwnController.class);
    @Autowired
    UserOwnServiceImpl userOwnService;
    @Autowired
    ProductServiceImpl productService;
    @PostMapping("/createUserOwn")
    public Result<HashMap<String,Object>> createUserOwn(@RequestBody HashMap<String,String> map){
        System.out.println(map.toString());
        Integer productId=Integer.parseInt(map.get("productId"));
        User loginUser= UserHolder.getUser();
        Integer userId=loginUser.getUserId();
        Map<String,Object> result=new HashMap<>();
        result.put("points",userOwnService.createUserOwn(productId,userId));
        result.put("products",productService.getAllListedProducts());
        return Result.success(result);
    }

    @GetMapping("/getByUserId")
    public Result<List<UserOwnDTO>> getByUserId(){
        //System.out.println("be called");
        User loginUser= UserHolder.getUser();
        logger.info("userID:{}",loginUser.getUserId());
        return Result.success(userOwnService.getByUserId(loginUser.getUserId()));
    }
}
